﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MessagePack.Unity;
using MessagePack;

public class Register {
	
	public void Init () {

		GameObject.Find ("Canvas/RegisterPanel/RegisterButton").GetComponent<Button> ().onClick.AddListener (RegisterUser);
		
	}
	
	void RegisterUser() {
		Debug.Log ("Register button clicked.");
	
		string name = GameObject.Find ("Canvas/RegisterPanel/UserNameInputField/Text").GetComponent<Text> ().text;
		string password = GameObject.Find ("Canvas/RegisterPanel/PasswordInputField/Text").GetComponent<Text>().text;

		if (string.IsNullOrEmpty(name)) {
			Debug.LogWarning("User name invalid.");
			return;
		} else if (string.IsNullOrEmpty(password)) {
			Debug.LogWarning("User password invalid.");
			return;
		}

		MsgRegister mr = new MsgRegister ();
		mr.Id = Protocal.register;
		mr.Name = name;
		mr.Password = password;

		byte[] data = MessagePackSerializer.Serialize(mr);

		NetworkManager.Instance.SendDataAsync (data, 0, data.Length, null, null);
	}
}
